package com.ebt.m.policy.presenter;

import android.content.Context;

import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.apibean.ProductItemsBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.ebt.m.policy.presenter.BrandsProductsPresenter.DATA_VIEW_TYPE;

/**
 * @author Damon.Han
 */
public class BrandsProductsAllPresenter extends BaseListPresenter<BaseListContract.IView> {
    public BrandsProductsAllPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        List<ProductItemsBean> list = (List<ProductItemsBean>) args[0];
        Observable.fromArray(list).compose(RxUtil.rxSchedulerHelper()).subscribe(new Observer<List<ProductItemsBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<ProductItemsBean> productItemsBeans) {
                iView.updateData(createSectionItem(productItemsBeans));
            }

            @Override
            public void onError(Throwable e) {
                iView.errorData();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null) {
            return sections;
        }
        for (int j = 0; j < list.size(); j++) {
            sections.add(new SectionItem(DATA_VIEW_TYPE, list.get(j)));
        }
        return sections;
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
    }

}
